package com.ujs.edu.demo.dao;

import com.ujs.edu.demo.dataobject.BcComment;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface BcCommentMapper {
    @Delete({
        "delete from bc_comment",
        "where comment_id = #{commentId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer commentId);

    @Insert({
        "insert into bc_comment (comment_id, member_id, ",
        "membername, memberImage, ",
        "createTime, score, ",
        "book_id, content)",
        "values (#{commentId,jdbcType=INTEGER}, #{memberId,jdbcType=INTEGER}, ",
        "#{membername,jdbcType=VARCHAR}, #{memberimage,jdbcType=VARCHAR}, ",
        "now(), #{score,jdbcType=INTEGER}, ",
        "#{bookId,jdbcType=INTEGER}, #{content,jdbcType=LONGVARCHAR})"
    })
    int insert(BcComment record);

    @Select({
        "select",
        "comment_id, member_id, membername, memberImage, createTime, score, book_id, ",
        "content",
        "from bc_comment",
        "where comment_id = #{commentId,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="comment_id", property="commentId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="member_id", property="memberId", jdbcType=JdbcType.INTEGER),
        @Result(column="membername", property="membername", jdbcType=JdbcType.VARCHAR),
        @Result(column="memberImage", property="memberimage", jdbcType=JdbcType.VARCHAR),
        @Result(column="createTime", property="createtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="score", property="score", jdbcType=JdbcType.INTEGER),
        @Result(column="book_id", property="bookId", jdbcType=JdbcType.INTEGER),
        @Result(column="content", property="content", jdbcType=JdbcType.LONGVARCHAR)
    })
    BcComment selectByPrimaryKey(Integer commentId);

    @Select({
        "select",
        "comment_id, member_id, membername, memberImage, createTime, score, book_id, ",
        "content",
        "from bc_comment"
    })
    @Results(id="commentMap",value = {
        @Result(column="comment_id", property="commentId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="member_id", property="memberId", jdbcType=JdbcType.INTEGER),
        @Result(column="membername", property="membername", jdbcType=JdbcType.VARCHAR),
        @Result(column="memberImage", property="memberimage", jdbcType=JdbcType.VARCHAR),
        @Result(column="createTime", property="createtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="score", property="score", jdbcType=JdbcType.INTEGER),
        @Result(column="book_id", property="bookId", jdbcType=JdbcType.INTEGER),
        @Result(column="content", property="content", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<BcComment> selectAll();

    //通过书籍id加载评论
    @Select({
            "select",
            "comment_id, member_id, membername, memberImage, createTime, score, book_id, ",
            "content",
            "from bc_comment",
            "where book_id=#{bookId}"
    })
    @ResultMap("commentMap")
    List<BcComment> selectByBookId(@Param("bookId") Integer bookId);


    @Update({
        "update bc_comment",
        "set member_id = #{memberId,jdbcType=INTEGER},",
          "membername = #{membername,jdbcType=VARCHAR},",
          "memberImage = #{memberimage,jdbcType=VARCHAR},",
          "createTime = #{createtime,jdbcType=TIMESTAMP},",
          "score = #{score,jdbcType=INTEGER},",
          "book_id = #{bookId,jdbcType=INTEGER},",
          "content = #{content,jdbcType=LONGVARCHAR}",
        "where comment_id = #{commentId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(BcComment record);
}